import 'package:flutter/material.dart';

class TweenAnimation extends StatefulWidget {
  TweenAnimation({this.text}) : super();
  final String? text;

  @override
  State<StatefulWidget> createState() {
    return TweenState();
  }
}

class TweenState extends State<TweenAnimation> with TickerProviderStateMixin {
  AnimationController? _controller;
  AnimationController? _controllerCurve;
  AnimationController? _custom;
  Color _textColor = Colors.white;
  AnimationController? _textColorController;
  Animation<Color>? _animation;

  //Curve
  Animation? _curveAnimation;
  Animation? _customAnimation;

  @override
  void initState() {
    super.initState();

    _controller =
        new AnimationController(duration: Duration(seconds: 5), vsync: this);

    _animation = ColorTween(begin: Colors.blue, end: Colors.red)
        .animate(_controller!) as Animation<Color>?;

    _controller?.addListener(() {
      setState(() {
        // _color = Color.lerp(_startColor, _endColor, _controller.value);
      });
    });
    //状态监听
    _controller?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _controller?.forward();
      }
    });
    //文字颜色
    _textColorController =
        new AnimationController(duration: Duration(seconds: 3), vsync: this);
    _textColorController?.addListener(() {
      setState(() {
        _textColor =
            Color.lerp(Colors.white, Colors.limeAccent, _controller!.value)!;
      });
    });
    //状态监听
    _textColorController?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _textColorController?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _textColorController?.forward();
      }
    });

    ///Curve
    _controllerCurve = new AnimationController(
        duration: Duration(milliseconds: 500), vsync: this);

    _curveAnimation = Tween(begin: 100.0, end: 200.0)
        .chain(CurveTween(curve: Curves.bounceIn))
        // .chain(CurveTween(curve: Curves.linear))
        // .chain(CurveTween(curve: Curves.bounceInOut))
        // .chain(CurveTween(curve: Curves.easeIn))
        .animate(_controllerCurve!);

    _curveAnimation?.addListener(() {
      setState(() {
        // _color = Color.lerp(_startColor, _endColor, _controller.value);
      });
    });
    //状态监听
    _curveAnimation?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controllerCurve?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _controllerCurve?.forward();
      }
    });

    //自定义
    ///Curve
    _custom = new AnimationController(
        duration: Duration(milliseconds: 500), vsync: this);

    _customAnimation = Tween(begin: 100.0, end: 200.0)
        .chain(CurveTween(curve: _StairsCurve(5)))
        .animate(_custom!);

    _custom?.addListener(() {
      setState(() {
        // _color = Color.lerp(_startColor, _endColor, _controller.value);
      });
    });
    //状态监听
    _custom?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _custom?.reverse();
      } else if (status == AnimationStatus.dismissed) {
        //动画恢复到初始状态时执行动画（正向）
        _custom?.forward();
      }
    });
    _custom?.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.text ?? ""),
      ),
      body: Center(
        // alignment: Alignment.center,
        child: Column(
          children: [
            SizedBox(
              height: 30,
            ),
            Align(
              alignment: Alignment.center,
              // child: Image.asset('images/iron_man.jpg', width: animation.value, height: animation.value),
              child: GestureDetector(
                  onTap: () {
                    // ToastUtils.toast("点我");
                    _controller?.forward();
                    _textColorController?.forward();
                  },
                  child: Container(
                    width: 100,
                    height: 100,
                    color: _animation?.value,
                    alignment: Alignment.center,
                    child: Text(
                      "点我变色",
                      // style: TextStyle(fontSize: 30, color: Colors.white),
                      style: TextStyle(fontSize: 10, color: _textColor),
                    ),
                  )),
            ),
            SizedBox(
              height: 50,
            ),
            Align(
              alignment: Alignment.center,
              // child: Image.asset('images/iron_man.jpg', width: animation.value, height: animation.value),
              child: GestureDetector(
                  onTap: () {
                    // ToastUtils.toast("点我");
                    _controllerCurve?.forward();
                  },
                  child: Container(
                    width: 200,
                    height: 200,
                    // color: Colors.blueAccent,
                    alignment: Alignment.center,
                    child: Image.asset('images/iron_man.jpg',
                        width: _curveAnimation?.value,
                        height: _curveAnimation?.value),
                  )),
            ),
            SizedBox(
              height: 50,
            ),
            Align(
              alignment: Alignment.center,
              // child: Image.asset('images/iron_man.jpg', width: animation.value, height: animation.value),
              child: Container(
                width: 200,
                height: 200,
                alignment: Alignment.center,
                child: Text(
                  "线条",
                  style:
                      TextStyle(fontSize: _custom?.value, color: Colors.black),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller?.dispose();
    _textColorController?.dispose();
    _controllerCurve?.dispose();
    super.dispose();
  }
}

class _StairsCurve extends Curve {
  //阶梯的数量
  final int num;
  double? _perStairY;
  double? _perStairX;

  _StairsCurve(this.num) {
    _perStairY = 1.0 / (num - 1);
    _perStairX = 1.0 / num;
  }

  @override
  double transform(double t) {
    return _perStairY! * (t / _perStairX!).floor();
  }
}
